Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

Description

Moves diagnostic generation/emit from the LibraryImportGenerator source generator to a new LibraryImportDiagnosticsAnalyzer in the same assembly, per Roslyn team recommendation.

Changes Made

1. Created LibraryImportDiagnosticsAnalyzer

  • Runs the same diagnostic logic as the generator
  • Uses SymbolEqualityComparer to compare attribute types instead of string matching
  • Creates LibraryImportGeneratorOptions once per compilation in RegisterCompilationStartAction
  • GetDiagnosticIfInvalidMethodForGeneration is internal static so the generator can call it directly

2. Updated LibraryImportGenerator

  • Removed diagnostic reporting from the pipeline
  • Calls LibraryImportDiagnosticsAnalyzer.GetDiagnosticIfInvalidMethodForGeneration directly to filter methods
  • No longer uses DiagnosticOr in the pipeline
  • Removed Diagnostics field from IncrementalStubGenerationContext

3. Updated CompilationExtensions.GetEnvironmentFlags

  • Changed to check for DisableRuntimeMarshallingAttribute on the source assembly instead of the module

4. Updated Test Infrastructure

  • Added TAnalyzer type parameter to CSharpSourceGeneratorVerifier
  • Added DisabledDiagnostics.Add(GeneratorDiagnostics.Ids.NotRecommendedGeneratedComInterfaceUsage) to CSharpAnalyzerVerifier
  • Tests using LibraryImportGenerator use LibraryImportDiagnosticsAnalyzer
  • Tests using other generators use EmptyDiagnosticAnalyzer

5. Updated Test Files

  • Diagnostics.cs: Uses CSharpAnalyzerVerifier (analyzer-only testing)
  • CompileFails.cs: Uses CSharpAnalyzerVerifier for diagnostic tests
  • ByValueContentsMarshalling.cs: Uses analyzer verifier with SYSLIB1092 re-enabled for specific tests

Test Results

  • LibraryImportGenerator.Unit.Tests: 702/703 pass (1 skipped)
  • ComInterfaceGenerator.Unit.Tests: 839/839 pass

Key Architectural Decisions

  1. Analyzer runs independently: The analyzer can run without the generator, making diagnostic-only tests faster
  2. Generator uses analyzer logic: The generator calls the analyzer's GetDiagnosticIfInvalidMethodForGeneration method to share validation logic
  3. Test separation: Tests that verify diagnostics run only the analyzer; tests that verify code generation run both

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits January 29, 2026 23:28
… diagnostics

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
…itional analyzer loading

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
…till reports diagnostics)

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copilot AI changed the title [WIP] Move diagnostic generation to analyzers alongside generators Add LibraryImportDiagnosticsAnalyzer foundation for diagnostic migration Jan 29, 2026
Copilot AI requested a review from jkoritzinsky January 29, 2026 23:53
jkoritzinsky

This comment was marked as outdated.

@jkoritzinsky jkoritzinsky dismissed their stale review January 30, 2026 00:15

Mis-clicked approve

@jkoritzinsky

This comment was marked as resolved.

Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot address this review.

…per review feedback

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copilot AI changed the title Add LibraryImportDiagnosticsAnalyzer foundation for diagnostic migration Move diagnostic generation from LibraryImportGenerator to LibraryImportDiagnosticsAnalyzer Jan 30, 2026
Copilot AI requested a review from jkoritzinsky January 30, 2026 00:47
…null, remove helper method

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
…analyzer only

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copy link
Member

@jkoritzinsky jkoritzinsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please address the feedback in this review.

…ion, simplify verifier, move options

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
@jkoritzinsky
Copy link
Member

@copilot Please adjust CompilationExtensions.GetEnvironmentFlags to check for DisableRuntimeMarshallingAttribute on the source assembly instead of the module. Also, add handling for the rest of the diagnostic edge cases for the remaining 5% of tests. Then, please remove the Diagnostics field from LibraryImportGenerator.IncrementalStubGenerationContext and remove downstream usages.

Copilot AI and others added 2 commits January 31, 2026 01:35
…y, remove Diagnostics from generator

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
…ortDiagnosticsAnalyzer

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 31, 2026

@copilot Please adjust CompilationExtensions.GetEnvironmentFlags to check for DisableRuntimeMarshallingAttribute on the source assembly instead of the module. Also, add handling for the rest of the di...

Addressed in commits f33ac74 and 9f85c66:

  1. CompilationExtensions.GetEnvironmentFlags - Updated to check compilation.Assembly.GetAttributes() instead of compilation.SourceModule.GetAttributes() for DisableRuntimeMarshallingAttribute.

  2. Removed Diagnostics field - Removed from IncrementalStubGenerationContext record and updated all downstream usages. The generator now uses discarding diagnostics bags and no longer returns diagnostics.

  3. Diagnostic edge cases - Fixed AddDisableRuntimeMarshallingAttributeFixerTests to use LibraryImportDiagnosticsAnalyzer. 28 tests still fail - the analyzer isn't reporting diagnostics for CompileFails.ValidateSnippets tests when run together with the generator. This appears to be related to how the analyzer's RegisterSymbolAction interacts with methods that have compile errors. Investigating further.

@jkoritzinsky
Copy link
Member

@copilot all of the test failures look like they're due to diagnostics reported by a marshalling generator resolver not being reported from the analyzers.

Copilot AI and others added 2 commits February 2, 2026 23:43
…c verification

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
…nalyzer verifier

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 2, 2026

@copilot all of the test failures look like they're due to diagnostics reported by a marshalling generator resolver not being reported from the analyzers.

Identified and partially fixed. The issue was that CSharpSourceGeneratorTest doesn't properly run analyzer diagnostics verification. Updated tests to use CSharpAnalyzerVerifier instead.

Commits eb6466c and cd49aed:

  • Changed CompileFails.ValidateSnippets to use analyzer verifier
  • Changed ByValueContentsMarshalling to use analyzer verifier
  • Removed CS8795 expectations (not relevant for analyzer-only tests)

Test status: 688/703 pass (14 failing). The 13 array marshalling tests are still failing because the analyzer returns 0 diagnostics for "Marshalling an array from unmanaged to managed requires..." cases. The ManagedToNativeStubGenerator in the analyzer may not be producing these diagnostics correctly. Investigating the marshalling generator resolver path.

Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants